package com.css.apps.base.user.action.login;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.validation.Assertion;

import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.WebBaseUtil;
import com.css.webbase.webwork.action.AbstractAction;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class SSO extends AbstractAction {
	
	public static final String CONST_SSO_LOGOUT = "_sso_logout_";

	private String toAction;
	private String logoutAction;
	public SUser sUser = new SUser();

	@SuppressWarnings("deprecation")
	public String go() {
		String loginName = "";
		Object object = get(AuthenticationFilter.CONST_CAS_ASSERTION);
		if (object != null) {
			Assertion assertion = (Assertion) object;
			loginName = assertion.getPrincipal().getName();
		}
		sUser = (SUser) WebBaseUtil.getCurrentUser();
		if (StringHelper.isNotEmpty(loginName)) {
			// sso=true,user=false----login 或者b顶掉a
			if (sUser == null || !loginName.equals(sUser.getLoginName())) {
				sUser.updateFailedLoginCount();
			}
			if (sUser == null) {
				set(AuthenticationFilter.CONST_CAS_ASSERTION, null);
				setMessage(Messages.getString("systemMsg.accountNotExist") + "[" + loginName + "]");
				return Action.ERROR;
			}
			set(Environment.SESSION_LOGIN_KEY, sUser);
			new Login().go();
			Cookie[] cookies = getRequst().getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (AuthenticationFilter.CONST_CAS_FROM.equals(cookie.getName()) && cookie.getMaxAge() != 0) {
						toAction = cookie.getValue();
						cookie.setMaxAge(0);
						cookie = null;
						break;
					}
				}
			}
			if (StringHelper.isEmpty(toAction)) {
				toAction = (String) getRequst().getParameter("toUrl");
			}
			if (StringHelper.isEmpty(toAction)) {
				toAction = (String) getRequst().getSession().getAttribute("toUrl");
			}
			if (StringHelper.isEmpty(toAction) || toAction.indexOf("sso.action") != -1) {
				toAction = "explorer.jsp";
			}
			try {
				toAction = URLDecoder.decode(toAction, "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			// SSO从client端登出处理
			logoutAction = getLogoutUrl();
			set(CONST_SSO_LOGOUT, logoutAction);
			return Action.SUCCESS;
		} else {
			// sso=false,user=true----quit
			if (sUser != null) {
				new Quit().go();
			}
			toAction = "login.jsp";
			return Action.SUCCESS;
		}
	}

	@SuppressWarnings("deprecation")
	public String getLogoutUrl() {
		String server = this.getSession().getServletContext().getInitParameter("sso.server.service");
		String client = this.getSession().getServletContext().getInitParameter("sso.client.serverName");
		// String server =
		// ConfigurationManager.getConfigurationManager().getSysConfigsso.server.serviceure("sso.server.service",
		// "");
		// String client =
		// ConfigurationManager.getConfigurationManager().getSysConfigure("sso.client.serverName",
		// "");
		if (StringHelper.isNotEmpty(server)) {
			server = server.replaceAll("\\\\", "");
			server = server + "/logout";
			HttpServletRequest request = ServletActionContext.getRequest();
			server = server + "?service=" + client + request.getContextPath();
		}
		return server;
	}

	public String getToAction() {
		return toAction;
	}

	public void setToAction(String toAction) {
		this.toAction = toAction;
	}

	public String getLogoutAction() {
		return logoutAction;
	}

	public void setLogoutAction(String logoutAction) {
		this.logoutAction = logoutAction;
	}
}
